package gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import controller.CtrEmployee;

public class UILogin extends JFrame
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private JTextField textField;
	
	private CtrEmployee ctrEmp;
	
	private boolean loggedIn;
	
	public UILogin() 
	{
		MainUI.getInstace().setVisible(false);
		
		loggedIn = false;
		ctrEmp = CtrEmployee.getInstance();
		
		getContentPane().setLayout(null);
		
		textField = new JTextField();
		textField.setBounds(10, 26, 92, 20);
		textField.setColumns(10);
		textField.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				login();
			}
		});
		getContentPane().add(textField);
		
		JButton btnOk = new JButton("Ok");
		btnOk.setBounds(115, 25, 50, 20);
		btnOk.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				login();
			}
		});
		
		getContentPane().add(btnOk);
		
		JLabel lblPassword = new JLabel("Password");
		lblPassword.setBounds(10, 10, 100, 20);
		getContentPane().add(lblPassword);
		
		setBounds(100,100, 190, 90);
		setVisible(true);
		
	}
	
	private void login()
	{
		if(ctrEmp.login(textField.getText())) {
			loggedIn = true;
			dispose();
			
			MainUI win = MainUI.getInstace();
			win.setVisible(true);
		}
		else {
			JOptionPane.showMessageDialog(this, "Incorrect Password!");
		}
	}
	
	public boolean isLoggedIn()
	{
		return loggedIn;
	}
}
